<?php
declare(strict_types=1);

namespace App\Service;

use App\Constants\StrStatus;
use App\Constants\XZXResponseCode;
use App\Exception\XZXValidationException;
use App\Model\Dao\TradeOrderBaseDao;
use App\Model\Dao\TradeOrderDetailDao;
use App\Model\Dao\TradeOrderLogDao;
use App\Model\Dao\TradeOrderReceiveDao;
use App\Model\Enum\OrderDetailDisEnum;
use App\Model\Enum\OrderDetailFrozenEnum;
use App\Model\Enum\OrderDetailRefundEnum;
use App\Model\Enum\OrderDetailSettleStatusEnum;
use App\Model\Enum\OrderDetailThawEnum;
use App\Model\Enum\OrderHandleStatusEnum;
use App\Model\Enum\OrderTransStatusEnum;
use App\Model\Enum\OrderTransTypeEnum;
use App\Model\Enum\PayTypeEnum;
use App\Model\Enum\TradeTrans\TradeTransTypeEnum;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use App\Utils\NumberUtils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Arr;


class TradeOrderDetailService
{
    /**
     * @Inject()
     * @var TradeOrderDetailDao
     */
    private $TradeOrderDetailDao;

    /**
     * @Inject()
     * @var TradeOrderBaseDao
     */
    private $TradeOrderBaseDao;

    /**
     * @Inject()
     * @var TradeOrderReceiveDao
     */
    private $TradeOrderReceiveDao;

    /**
     * @Inject()
     * @var TradeOrderLogDao
     */
    private $TradeOrderLogDao;

    public function get($param)
    {
        $info = $this->TradeOrderDetailDao->get($param['id']);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        if($param['source'] == 2 && !$param['isSuperRole']){
            if(!in_array($info['mchId'],$param['mchIdList'])){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        if($param['source'] == 1){
            if($info['mchId'] != $param['mchId']){
                throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::NO_PERMISSION_TO_OPERATE));
            }
        }

        $info['strIsDis']       = OrderDetailDisEnum::getLabel($info['isDis']);
        $info['strIsFrozen']    = OrderDetailFrozenEnum::getLabel($info['isFrozen']);
        $info['strIsRefund']    = OrderDetailRefundEnum::getLabel($info['isRefund']);
        $info['strIsSettle']    = OrderDetailSettleStatusEnum::getLabel($info['isSettle']);
        $info['strIsThaw']      = OrderDetailThawEnum::getLabel($info['isThaw']);
        $info['strTransStatus'] = OrderTransStatusEnum::getLabel($info['transStatus']);

        // 查base
        $base = $this->TradeOrderBaseDao->getByOrderId($param['id']);

        // 查receive
        $receive = $this->TradeOrderReceiveDao->getByOrderId($param['id']);

        (isset($info['createTime']) && !empty($info['createTime'])) && $info['createTime'] = DateUtils::getDateTime($info['createTime']);
        (isset($info['channelTransTime']) && !empty($info['channelTransTime'])) && $info['channelTransTime'] = DateUtils::getDateTime($info['channelTransTime']);
        (isset($info['settleTime']) && !empty($info['settleTime'])) && $info['settleTime'] = DateUtils::getDateTime($info['settleTime']);
        (isset($info['mchTransTime']) && !empty($info['mchTransTime'])) && $info['mchTransTime'] = DateUtils::getDateTime($info['mchTransTime']);

        (isset($base['createTime']) && !empty($base['createTime'])) && $base['createTime'] = DateUtils::getDateTime($base['createTime']);
        (isset($receive['createTime']) && !empty($receive['createTime'])) && $receive['createTime'] = DateUtils::getDateTime($receive['createTime']);
        ($param['source'] == 1 && isset($receive['cardNo']) && !empty($receive['cardNo'])) && $receive['cardNo'] = substr($receive['cardNo'], 0, 6) . '****' . substr($receive['cardNo'], -4);

        $result = $info;
        $result['base'] = $base;
        $result['receive'] = $receive;

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];

        $total = $this->TradeOrderDetailDao->getCount($param);
        $list = $this->TradeOrderDetailDao->getList($param, $page, $limit);

        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            (isset($v['mchTransTime']) && !empty($v['mchTransTime'])) && $v['mchTransTime'] = DateUtils::getDateTime($v['mchTransTime']);
            (isset($v['channelTransTime']) && !empty($v['channelTransTime'])) && $v['channelTransTime'] = DateUtils::getDateTime($v['channelTransTime']);
            (isset($v['settleTime']) && !empty($v['settleTime'])) && $v['settleTime'] = DateUtils::getDateTime($v['settleTime']);

            $v['floatMchTransAmount']  = NumberUtils::centToYuan($v['mchTransAmount']);
            $v['floatSettleAmount']    = NumberUtils::centToYuan($v['settleAmount']);
            $v['floatChannelTransAmount'] = NumberUtils::centToYuan($v['channelTransAmount']);
            $v['floatServiceFee']      = NumberUtils::centToYuan($v['serviceFee']);
            $v['floatPerFee']          = NumberUtils::centToYuan($v['perFee']);
            $v['floatBondAmount']      = NumberUtils::centToYuan($v['bondAmount']);
            $v['floatDisAmount']       = NumberUtils::centToYuan($v['disAmount']);
            $v['floatRefundAmount']    = NumberUtils::centToYuan($v['refundAmount']);
            $v['floatFrozenAmount']    = NumberUtils::centToYuan($v['frozenAmount']);

//            $v['strIsDis'] = StrStatus::TRADE_ORDER_DETAIL_IS_DIS[$v['isDis']];
//            $v['strIsFrozen'] = StrStatus::TRADE_ORDER_DETAIL_IS_FROZEN[$v['isFrozen']];
//            $v['strIsRefund'] = StrStatus::TRADE_ORDER_DETAIL_IS_REFUND[$v['isRefund']];
//            $v['strIsSettle'] = StrStatus::TRADE_ORDER_DETAIL_IS_SETTLE[$v['isSettle']];
//            $v['strIsThaw'] = StrStatus::TRADE_ORDER_DETAIL_IS_THAW[$v['isThaw']];
            $v['strIsDis']       = OrderDetailDisEnum::getLabel($v['isDis']);
            $v['strIsFrozen']    = OrderDetailFrozenEnum::getLabel($v['isFrozen']);
            $v['strIsRefund']    = OrderDetailRefundEnum::getLabel($v['isRefund']);
            $v['strIsSettle']    = OrderDetailSettleStatusEnum::getLabel($v['isSettle']);
            $v['strIsThaw']      = OrderDetailThawEnum::getLabel($v['isThaw']);
            $v['strTransStatus'] = OrderTransStatusEnum::getLabel($v['transStatus']);
            $v['strChannelType'] = PayTypeEnum::getLabel($v['channelType']);


            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }

    public function log($param) {
        $result = $this->TradeOrderLogDao->getWhereList(['orderId' => $param['id']], ['createTime' => 'desc']);
        foreach ($result as $k => $v) {
            if(isset($v['createTime'])){
                $v['createTime'] = DateUtils::getDateTime($v['createTime']);
                $v['strHandleStatus'] = OrderHandleStatusEnum::getLabel($v['handleStatus']);
                $v['strTransType'] = OrderTransTypeEnum::getLabel($v['transType']);
            }
            $result[$k] = $v;
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function disRateList ($param) {

        $page  = Arr::pull($param, 'page');
        $limit = Arr::pull($param, 'limit');

        $data = $this->TradeOrderDetailDao->getDisRateList($param, $page, $limit);

        return CustomUtils::buildSuccess($data);
    }
}
